﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using MyOA.BLL;
using MyOA.Model;
namespace MyOA.UI.File.FileManage
{
    public partial class OperateFolder : System.Web.UI.Page
    {
        OperateFolderManage manage = new OperateFolderManage();
        /// <summary>
        /// 修改时数据绑定
        /// </summary>
        protected void updateDataBind(FileInfo fileInfo)
        {
            txtFileName.Text = fileInfo.FilesName;
            lblPath.Text = fileInfo.FilePath;
            txtRemark.Text = fileInfo.Remark;
            lblCreateTime.Text = fileInfo.CreateDate.ToString();
            lblUser.Text = fileInfo.FileOwner.LoginId;
        }

        /// <summary>
        /// 增加时数据绑定
        /// </summary>
        protected void insertDataBind(string path)
        {
            lblCreateTime.Text = DateTime.Now.ToString("yy-MM-dd hh:mm:ss");
            lblUser.Text = (string)Session["loginId"];
            lblPath.Text = path;
        }

        /// <summary>
        /// 构建文件信息
        /// </summary>
        /// <returns></returns>
        protected FileInfo createFileInfo()
        {
            FileInfo fileInfo = new FileInfo();
            fileInfo.FilesName = txtFileName.Text;
            fileInfo.FilePath = lblPath.Text;
            fileInfo.Remark = txtRemark.Text;
            fileInfo.CreateDate = DateTime.Parse(lblCreateTime.Text);
            
             //增加时才用到USERID
            if (Request.QueryString["fileId"] == null)
            {
                fileInfo.FileOwner.UserId = manage.getUserIdByLoginId(lblUser.Text);
            }
                //修改时 用到FILEID
            else
            {
                fileInfo.FileId = int.Parse(Request.QueryString["fileId"]);
            }

            //(parentId增加时才获取)
            if (Request.QueryString["parentId"] != null)
            {
                fileInfo.ParentId = int.Parse(Request.QueryString["parentId"]);
            }
                return fileInfo;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["loginId"] == null)
            {
                Response.Redirect("~/Login.aspx");
                return;
            }
            if (!IsPostBack)
            {
                //修改
                if (Request.QueryString["fileId"] != null)
                {
                    int fileId = int.Parse(Request.QueryString["fileId"]);
                    updateDataBind(manage.getFileInfoByFileId(fileId));
                }
                //增加
                else
                {
                    string path = Server.UrlDecode(Request.QueryString["path"]);
                    insertDataBind(path);
                }

            }

        }

        /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            //修改
            if (Request.QueryString["fileId"] != null)
            {
                int fileId = int.Parse(Request.QueryString["fileId"]);
                int parentId = manage.getParentIdByFileId(fileId);
                if (manage.updateFileInfoByFileId(createFileInfo()))
                {
                    string script = string.Format("<script>Alert('修改成功',null,null,'温馨提示');parent.document.getElementById('ifrFileLeft').src='FileLeft.aspx?id={0}';document.location.href='FileMain.aspx?fileId={1}'</script>", DateTime.Now.Ticks.ToString(),parentId);
                    ScriptManager.RegisterStartupScript(UpdatePanel1,this.GetType(),"",script,false);
                }
                else
                {
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "", "修改失败", false);
                }
            }
                //增加
            else
            {
                if (manage.insertFileInfo(createFileInfo()))
                {
                    string script = string.Format("<script>Alert('增加成功',null,null,'温馨提示');parent.document.getElementById('ifrFileLeft').src='FileLeft.aspx?id={0}';document.location.href='FileMain.aspx?fileId={1}'</script>", DateTime.Now.Ticks.ToString(), Request.QueryString["parentId"]);
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "", script, false);
                }
                else
                {
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "", "增加失败", false);
                }
            }
        }

        /// <summary>
        /// 退出按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            int parentId = 0;
            //修改
            if (Request.QueryString["fileId"] != null)
            {
                int fileId = int.Parse(Request.QueryString["fileId"]);
                parentId = manage.getParentIdByFileId(fileId);
            }
            //增加
            else
            {
                parentId = int.Parse(Request.QueryString["parentId"]);
            }

            Response.Redirect("FileMain.aspx?fileId=" + parentId);

        }
    }
}
